List<CommentTreeNode> convertJson2Node(List<dynamic> data) {
  if (data == null) return [];
  List<CommentTreeNode> result = [];
  for (var item in data) {
    result.add(CommentTreeNode.fromJson(item));
  }
  return result;
}

class CommentTree {
  String videoId;
  List<CommentTreeNode> commentTreeNodes;

  CommentTree(this.videoId);

  CommentTree.test(): videoId = "0", commentTreeNodes = List.of(
      [CommentTreeNode.test(), CommentTreeNode.test(), CommentTreeNode.test(), CommentTreeNode.test(), CommentTreeNode.test()]
  );

   CommentTree.fromJson(Map<String, dynamic> json) {
    // print(json['commentTreeNodes']);
    videoId = json['videoId'];
    commentTreeNodes = convertJson2Node(json['commentTreeNodes']);
  }
  CommentTree.empty(): videoId = "", commentTreeNodes = [];

}

class CommentTreeNode {
    int commentId;
    int parentId;
    String content;
    int authorId;
    int likeCount;
    DateTime commentTime;
    bool isDeleted;
    int replyId;
    List<CommentTreeNode> child;
    bool isLike = false;
    bool isDislike = false;
    String userName;
    String userProfile;

    CommentTreeNode(
      this.commentId,
      this.parentId,
      this.content,
      this.authorId,
      this.likeCount,
      this.commentTime,
      this.isDeleted,
      this.replyId,
      this.child);

    CommentTreeNode.test():
    commentId = 1,
    parentId = 1,
    content = "测试评论",
    authorId = 0,
    likeCount = 11,
    commentTime = DateTime(2023),
    isDeleted = false,
    replyId = 0;

    CommentTreeNode.test1():
          commentId = 1,
          parentId = 1,
          content = "你好",
          authorId = 0,
          likeCount = 11,
          commentTime = DateTime.now(),
          isDeleted = false,
          replyId = 0;



    CommentTreeNode.fromJson(Map<String, dynamic> json) {
      // return CommentTreeNode(json['commentId'], json['parentId'],
      //     json['content'], json['authorId'], json['likeCount'],
      //     json['commentTime'], json['isDeleted'], json['replyId'],
      //     json['child']);
      commentId = int.parse(json['commentId']);
      parentId = int.parse(json['parentId']);
      content = json['content'];
      authorId = int.parse(json['authorId']);
      likeCount = int.parse(json['likeCount']);
      // print('fromJson transform : ${json['commentTime']} ${json['commentTime'].runtimeType}');
      commentTime = DateTime.parse(json['commentTime']);
      isDeleted = json['isDeleted'];
      replyId = json['replyId'] == null ? 0 : int.parse(json['replyId']);
      child = convertJson2Node(json['child']);
    }
}